﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using Housing;

public partial class ManageCenter_FinallyCheck : System.Web.UI.Page
{
    Apply apply = new Apply();
    protected void Page_Load(object sender, EventArgs e)
    {
        BindDate();
        if (Request["id"] != null)
        {
            lblnoid.Text = Request.QueryString["id"].ToString();
            SubsidyRecord subsidy = new SubsidyRecord();
            subsidy.GetNewestSubsidyRecordByNoid(lblnoid.Text);
            if (subsidy.Adj_stoptime != null)
            {
                ckbsub.Checked = true;
                txbsubarea.Text = subsidy.Adj_nowarea;
                txbsubmoney.Text = subsidy.Adj_nowmoney;
                ddlyear1.SelectedValue = subsidy.Adj_stoptime.Substring(0, 4);
                if(subsidy.Adj_stoptime.Substring(4,1)=="0")
                    ddlmonth1.SelectedValue = subsidy.Adj_stoptime.Substring(5, 1);
                else
                    ddlmonth1.SelectedValue = subsidy.Adj_stoptime.Substring(4, 2);

                //txbsubstart.Text = drInd["ind_subbegindate"].ToString();
            }
            ReduceRecord reduce = new ReduceRecord();
            reduce.GetNewestReduceRecordBynoid(lblnoid.Text);
            if (reduce.Adj_stoptime != null)
            {
                ckbred.Checked = true;
                txbredname.Text = reduce.Adj_renter;
                txbredaddress.Text = reduce.Adj_rentaddress;
                txbredcheckarea.Text = reduce.Adj_nowarea;
                txbredcheckmoney.Text = reduce.Adj_nowmoney;
                txbredmonthmoney.Text = reduce.Adj_nowreduce;
                ddlyear2.SelectedValue = reduce.Adj_stoptime.Substring(0, 4);
                if (reduce.Adj_stoptime.Substring(4, 1)=="0")
                    ddlmonth2.SelectedValue = reduce.Adj_stoptime.Substring(5, 1);
                else
                    ddlmonth2.SelectedValue = reduce.Adj_stoptime.Substring(4, 2);
            }
            KindRecord kind = new KindRecord();
            kind.GetNewestKindRecordBynoid(lblnoid.Text);
            if (kind.Adj_stoptime != null)
            {
                ckbkind.Checked = true;
                txbkindaddress.Text = kind.Adj_location;
                txbkindarea.Text = kind.Adj_nowarea;
                txbkinduserarea.Text = kind.Adj_nowuserarea;
                ddlyear3.SelectedValue = kind.Adj_stoptime.Substring(0, 4);
                if (kind.Adj_stoptime.Substring(4, 1) == "0")
                    ddlmonth3.SelectedValue = kind.Adj_stoptime.Substring(5, 1);
                else
                    ddlmonth3.SelectedValue = kind.Adj_stoptime.Substring(4, 2);
            }
        }
        else
            WebHelper.AlertAndClose("地址链接错误，请重新进入！");
    }
    protected void btnunpass_Click(object sender, EventArgs e)
    {
        bool fail = apply.SetApplyUnPass(lblnoid.Text);
        if (fail)
            WebHelper.AlertAndCloseThenRefreshLastPage("该申请单已被否决！");
        else
            WebHelper.Alert("否决不通过，请重新操作");
    }
    protected void btnpass_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(txbdate.Text))
        {
            string longid = apply.GenerateLongId(lblnoid.Text);
            bool succAddLongId = apply.AddLongIdToApply(lblnoid.Text, longid);
            if (succAddLongId)
            {
                FinnalyCheck finnalycheck = new FinnalyCheck();
                bool succAddFinCheck = finnalycheck.AddFinCheck(lblnoid.Text, txbname.Text, txbreponsname.Text, txbdate.Text);
                if (succAddFinCheck)
                {
                    bool succSetApplyPass = apply.SetApplyPass(lblnoid.Text, longid);
                    if (succSetApplyPass)
                        WebHelper.AlertAndCloseThenRefreshLastPage("审核成功，请继续其他操作！");
                    else
                        WebHelper.Alert("审核失败，请重新操作");
                }
                else
                    WebHelper.Alert("已审核或审核失败！");
            }
            else
                WebHelper.Alert("生成长编号失败，请重新操作");
        }
        else
            WebHelper.Alert("请填写审核日期");
    }
    protected void BindDate()
    {
        int year = Convert.ToInt32(DateTime.Now.Year.ToString());
        for (int i = year; i < year + 3; i++)
        {
            ddlyear1.Items.Add(i.ToString());
            ddlyear2.Items.Add(i.ToString());
            ddlyear3.Items.Add(i.ToString());
        }
        for (int i = 1; i < 13; i++)
        {
            ddlmonth1.Items.Add(i.ToString());
            ddlmonth2.Items.Add(i.ToString());
            ddlmonth3.Items.Add(i.ToString());
        }
        ddlyear1.SelectedValue = DateTime.Now.Year.ToString();
        ddlyear2.SelectedValue = DateTime.Now.Year.ToString();
        ddlyear3.SelectedValue = DateTime.Now.Year.ToString();

        ddlmonth1.SelectedValue = DateTime.Now.Month.ToString();
        ddlmonth2.SelectedValue = DateTime.Now.Month.ToString();
        ddlmonth3.SelectedValue = DateTime.Now.Month.ToString();
    }
}
